#include "radar.h"

#include "terrain.h"
#include "snowman.h"
#include "helicopter.h"
#include "hat.h"
#include "player.h"

CRadar::CRadar()
{
	radius=64.0f;
	scl = 1.0f / 25.0f;
}

//<<moveTo>>
//changes current point
void CRadar:: moveTo(float x, float y)
{
	oldX = x;
	oldY = y;
}

//<<lineTo>>
//draws a line from current point to new point
void CRadar:: lineTo(float x, float y)
{
	glBegin(GL_LINES);
		glVertex2f(oldX, oldY);
		glVertex2f(x, y); //draw the line
	glEnd();
	oldX = x; //update current point to new point
	oldY = y;
}

void CRadar::Draw(int windowHeight, CTerrain* terrain, CVector* playerPos, float* playerAngle )
{
	xCtr = 75.0f;
	yCtr = windowHeight - 75.0f;

	drawPt(CENTER, xCtr, yCtr);
	glColor3f(0.0, 0.0, 0.0);
	drawCross();
	drawRadar();

	//Art O.  --> Draw markers

	CNode* node;
	//Draw Snowmen markers on radar
	node = terrain->childNode;
	while (!node->IsLastChild())
	{
		if (typeid(*node) == typeid(CSnowman))
		{
			CSnowman* enemy = (CSnowman*)node;
			DrawMarker( ENEMY, &enemy->position, playerPos, playerAngle );
		}
		node = node->nextNode;
	}

	//Draw Helicopter marker on radar
	node = terrain->childNode;
	while (!node->IsLastChild())
	{
		if (typeid(*node) == typeid(Helicopter))
		{
			Helicopter* heli = (Helicopter*)node;
			DrawMarker( HELICOPTER, &heli->position, playerPos, playerAngle );
		}
		node = node->nextNode;
	}

	//Draw Hat markers on radar
	node = terrain->childNode;
	while (!node->IsLastChild())
	{
		if (typeid(*node) == typeid(Hat))
		{
			Hat* hat = (Hat*)node;
			DrawMarker( HAT, &hat->position, playerPos, playerAngle );
		}
		node = node->nextNode;
	}
}

void CRadar::DrawMarker( int type, CVector* markerPos, CVector* playerPos, float* playerAngle )
{
	float pX = markerPos->x;
	float pZ = markerPos->z;

	//Rotate the point based on player camera angle
	float s = sin(-(*playerAngle-90) * (PI / 180));
	float c = cos(-(*playerAngle-90) * (PI / 180));
	pX -= playerPos->x;				// translate point back to origin
	pZ -= playerPos->z;
	float xnew = pX * c - pZ * s;	// rotate point
	float ynew = pX * s + pZ * c;
	pX = xnew + playerPos->x;		// translate point back
	pZ = ynew + playerPos->z;

	//Calculate distance of point to see if it's on the radar
	float dx = (pX - playerPos->x) * scl;
	float dz = (pZ - playerPos->z) * scl;
	float dist = sqrt(dx*dx + dz*dz);

	if(dist < radius)
		drawPt(type, xCtr - dx, yCtr - dz);
}

void CRadar::drawCross()
{
	// Note: Size represents the radius of the circle

	moveTo(xCtr, yCtr + radius);								// Start at the center, then move up (90.0 deg)
	lineTo(xCtr, yCtr - radius);								// Now move down (270.0 deg)

	moveTo(xCtr + radius, yCtr);								// Start at the center, move to the right (0.0 deg)
	lineTo(xCtr - radius, yCtr);								// Now move left (180.0 deg)
}


void CRadar::drawRadar()									// Draws a circle
{
	float revAngle = 360.0;

	float angle = revAngle * PI / 180;  // initial angle
	float angleInc = 2 * PI / 32.0;         //angle increment
	glColor3f(0.0, 0.0, 7.0);
	moveTo(radius * cos(angle) + xCtr, radius * sin(angle) + yCtr);
	for(int k = 0; k < 32; k++)  // repeat n times
	{
		angle += angleInc; 
		lineTo(radius * cos(angle) + xCtr, radius * sin(angle) + yCtr);
	}
}

void CRadar::colorPt(float r, float g, float b)
{
	glColor3f(r, g, b);
}

void CRadar::drawPt(int type, float x, float y)
{
	switch(type)
	{
	case ENEMY:
		colorPt(15.0, 0.0, 0.0);
		break;
	case CENTER:
		colorPt(0.0, 15.0, 0.0);
		break;
	case HELICOPTER:
		colorPt(0.0, 0.0, 0.0);
		break;
	case HAT:
		colorPt(0.0, 0.0, 15.0);
		break;
	}
	glPointSize(7.0);
	glBegin(GL_POINTS);
		glVertex2f(x, y);
	glEnd();

}